package com.java.basis.io.nio.one;

import org.hibernate.engine.jdbc.spi.SchemaNameResolver;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * 目标：客户端实现，基于NIO非阻塞式通信
 *
 * @author wangning
 * @create 2022-01-11 11:27
 */
public class Client {
	public static void main(String[] args) throws Exception {

		//1.获取通道
		SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(9999));
		//2.切换非阻塞式模式
		socketChannel.configureBlocking(false);
		//3.分配指定缓冲区的大小
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		//4.发送数据给服务端
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("请输入：");
			String msg = scanner.nextLine();
			buffer.put(("Aa：" + msg).getBytes());
			buffer.flip();
			socketChannel.write(buffer);
			buffer.clear();
		}
	}
}
